package com.liugh.controller.zhongche;

import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.base.BusinessException;
import com.liugh.base.PublicResultConstant;
import com.liugh.config.ResponseModel;
import com.liugh.controller.BaseRestController;
import com.liugh.entity.standard.Department;
import com.liugh.service.IUserService;
import com.liugh.service.zhongche.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**部门模块 .
 * Created by Fatao on 2018/11/9 .
 */


@Api(description = "部门管理")
@RestController
@RequestMapping("/department")
public class DepartmentController extends BaseRestController {


    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IUserService userService;


    /**
     * 添加一个部门 .
     * @param department .
     * @return .
     * @throws BusinessException .
     */
    @ApiOperation("添加一个部门")
    @PostMapping("/add")
    public ResponseModel<Department> addDepartment(@RequestBody Department department) throws BusinessException {
        department = departmentService.addDepartment(department);
        if (department == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        } else {
            return fillSuccessData(department);
        }
    }

    /**
     * 删除一个部门 .
     * @param id .
     * @return .
     */

    @ApiOperation("删除部门")
    @DeleteMapping("/delete")
    public ResponseModel<String> deleteDepartmentById(@RequestParam("id") int id) throws BusinessException {
        if (userService.getUserbyDepartmentId(id).isEmpty()) {
            int result = departmentService.deleteDepartment(id);
            if (result > 0) {
                return fillSuccessData(PublicResultConstant.SUCCEED);
            } else {
                throw new BusinessException(PublicResultConstant.ERROR);
            }
        } else {
            return fillFailData(PublicResultConstant.DEPARETMENT_NOT_NULL);
        }
    }


    /**
     * 更新部门 .
     * @param department .
     * @return .
     * @throws BusinessException .
     */
    @ApiOperation("更新部门信息")
    @PutMapping("/update")
    public ResponseModel<Department> updateDepartment(@RequestBody Department department) throws BusinessException {
        department = departmentService.updateDepartment(department);
        if (department == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        } else {
            return fillSuccessData(department);
        }
    }


    /**
     * 根据id获取部门 .
     * @param id .
     * @return .
     */
    @ApiOperation("根据id获取部门")
    @GetMapping("/get")
    public ResponseModel<Department> getDepartmentById(@RequestParam("id") int id) {
        return fillSuccessData(departmentService.getDepartmentById(id));
    }




    /**
     * 获取所有部门 .
     * @return .
     */
    @ApiOperation("获取所有部门")
    @GetMapping("/get_all")
    public ResponseModel<Page<Department>> getDepartmentById(@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,
                                                             @RequestParam(value = "pageSize",required = false,defaultValue = "10") int pageSize) {
        return fillSuccessDataPage(departmentService.getAllDepartment(new Page<>(pageNum, pageSize)));
    }




}
